package com.corecloud.controller.sms;

import com.corecloud.entity.ValidateCode;
import com.corecloud.service.sms.ValidateCodeGenerator;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;


@Component
public class SmsCodeGenerator implements ValidateCodeGenerator {

    private static final String SMS_CODE_PARAM_NAME = "mobile";

    /**
     * 验证码生成
     * @param httpServletRequest
     * @return
     */
    @Override
    public ValidateCode generator(ServletWebRequest httpServletRequest) {
//        产生6位长度的随机字符串，中文环境下是乱码
        String code = RandomStringUtils.randomNumeric(6);
//        设置验证码码时效时间
        return new SmsCode(httpServletRequest.getRequest().getParameter("mobile"),code, 60*15);
    }
}
